define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Activitysecond, $stateParams, $timeout, GoPage, GoDialog, Auth, ChatInfo, $ionicModal,$sce) {
		
		//画面加载
		$scope.$on('$ionicView.afterEnter', function() {
			$scope.act_id = $stateParams.act_id;
			$scope.as_id = $stateParams.as_id;//8和9用到
			$scope.init_data();
		}, false);
		
		/*
		 * 9个活动
		 * 活动类型
			1=>团购活动
			2=>秒杀活动
			3=>限时抢购活动
			4=>拍卖活动
			5=>新品测试活动
			6=>新品赠送活动
			7=>新品公告活动
			8=>产品推广活动(直播)
			9=>产品促销活动
			有的活动有三级页面，有的只有二级
		*/
		function getMethod(){
			$scope.activitType = $stateParams.type;//html据此判断群的显示
			if($stateParams.type=='1') {
				$scope.title = "疯狂团购";
				$scope.detailUrl = "activitythree";
				$scope.method = "marketing.groupbuy.viewdetail";
			}
			if($stateParams.type=='2') {
				$scope.title = "限时秒杀";
				$scope.detailUrl = "activitythree";
				$scope.method = "marketing.seckill.view";
			}
			if($stateParams.type=='3') {
				$scope.title = "限时抢购";
				$scope.detailUrl = "activitythree";
				$scope.method = "marketing.panicbuy.view";
			}
			if($stateParams.type=='8'){//如果是8和9，则跳转到商品详情
				$scope.title = "直播";
				$scope.detailUrl = "productdetail";//promotionslive/:act_id/:as_id
				$scope.method = "video.live.get";
				$scope.chatDetail = "promotionslive";
			}
			if($stateParams.type=='9'){
				$scope.title = "商品促销";
				$scope.detailUrl = "productdetail";//promotionspicture/:act_id/:as_id
				$scope.method = "video.live.get";
				$scope.chatDetail = "promotionspicture";
			}
		}
		
		//加载数据
		$scope.init_data = function() {
			getMethod();
			Activitysecond.promotion_detail(1,$scope.act_id,$scope.as_id,$scope.method, function(data) {
				console.log(data.result)
				if(!data.success) {
					GoDialog.alert("提示",data.msg,function(){
						GoPage.go_back();
					});
					return;
				}
				$scope.datas = data.result;
				$scope.get_loginfo();
				$scope.isLoad = false;
				$scope.nPage = 1;
				$timeout(function(){//延时再处理，避免页面没有渲染完
					var iframeHeight = 0;//iframe的高度
					/****视频处理****/
					if(data.result.act_promotion_type=='media'){
						var objE = document.createElement("div");
				        objE.innerHTML = data.result.video_code;
				        $scope.myURL = $sce.trustAsResourceUrl(objE.children[0].getAttribute("src"));
//				        iframeHeight = document.getElementById("videoContainer").clientHeight;
						iframeHeight = 7.5;
					}
					/****直播处理****/
					if(data.result.act_promotion_type=='live'&&data.result.token){
						$scope.makeplayer(data.result.token);//根据token创建播放器
//						iframeHeight = document.getElementById("playerContainer").clientHeight;
						iframeHeight = 7.5;
					}
					$scope.playerContainer = {
						"margin-top": iframeHeight+ "rem",
					};
				},1000)
			});
		}
		
		//视频播放
		$scope.makeplayer = function(token) {
			var opts = {
				"playerId": "gotyeplayer", //生成视频dom对象id,可为空,默认为gotyeswf
				"width": "100%", //视频宽,如 '600px' 或者 '50%', 默认'100%'
				"height": "7.5rem", //视频高,如 '400px' 或者 '50%', 默认'100%'
				"autoplay": false, //默认true,是否自动播放
				"bg":$scope.datas.image_cover//'./img/bg-z-cf.png'
			};
			$scope.player = new Gotye.Player(token, opts);
			$scope.player.onFinish(function() {
				console.log("播放器加载成功");
			});
			$scope.player.onPlayUserChange(function(count, virCount) {
				console.log("当前观看人数为：" + count);
				console.log("当前人数模拟值为（生成规则请见管理后台房间配置）：" + virCount);
				$scope.playerNum = count;
			});
			$scope.player.onPubStatusChange(function(status) {
				console.log("主播端推流状态：" + status);
				$scope.playering = status;
			});
			$scope.player.onForceLogout(function() {
				console.log("您已经被踢出");
				GoDialog.alert("提示", "您已经被踢出",function(){
					GoPage.go_back();
				});
			});
			$scope.player.appendTo("playerWapper");//div的id
		};
		
		
		//跳转到商品详情
		$scope.go_goods = function(goods_id, product_id) {
			var type = $scope.activitType;
			if(type==8||type==9){//跳转到商品详情
				type = "goods";
			}
			GoPage.go_states($scope.detailUrl, {
				'goods_id': goods_id,
				'product_id': product_id,
				'act_id': $scope.act_id,
				'as_id': $scope.as_id,
				"type":type
			});
		}
		
		//跳转到聊天页面。
		$scope.go_exchangechat = function(group_id, is_member) {
			if($scope.activitType==8||$scope.activitType==9){
				GoPage.go_states($scope.chatDetail, {'act_id': $scope.act_id,'as_id':$scope.as_id});
			}
			if(is_member!=true){
				Activitysecond.join_group(group_id, function(data) {
					if(data.rtnCode == 0 || data.rtnCode == 5) {
						GoPage.go_states("exchange_interaction_chat", {
							'group_id': group_id
						});
					}else {
						GoDialog.alert("提示",data.rtnMsg);
					}
				})
			}else{
				GoPage.go_states("exchange_interaction_chat", {
					'group_id': group_id
				});
			}
		}
		
		//小小图登录信息
		$scope.get_loginfo = function() {
			//判断本地是否保存有小小图登录信息
			var is_chatLogin = ChatInfo.get_isInfos();
			if(is_chatLogin == false && Auth.check_loginInfo())
				Activitysecond.get_loginfo(function(data) {
					$scope.chat_url = data.chat_url;
					$scope.login_code = data.login_code;
					$scope.soket_url = data.soket_url;
					//初次保存小小图登录信息
					ChatInfo.save_chat_loginInfo($scope.chat_url, $scope.login_code, $scope.soket_url);
				})
		};
		
		/***************活动简介**************/
		$ionicModal.fromTemplateUrl("templates/modal/activityIntroduction.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_intro = modal;
		});
		//打开
		$scope.openModal_intro = function() {
			if(!$scope.datas||!$scope.datas.introduction){
				GoDialog.alert("提示","暂时没有活动介绍哦！");
				return;
			}
			$scope.modal_intro.show();
		}
		//隐藏
		$scope.closeModal_intro = function() {
			$scope.modal_intro.hide();
		}
		/***************活动简介**************/
		
		
		/*===================下拉刷新 ===============*/
		$scope.doRefresh = function() {
			$scope.isLoad = true;
			Activitysecond.promotion_detail(1,$scope.act_id,$scope.as_id,$scope.method, function(data) {
				$scope.datas = data.result;
				$scope.nPage = 1;
			},function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		};
		//加载更多
		$scope.doRefreshMore = function() {
			$scope.isLoad = true;
			Activitysecond.promotion_detail($scope.nPage+1,$scope.act_id,$scope.as_id,$scope.method, function(data) {
//				console.log(data)
				if(data != null&&data.result&&data.result.goods) {
					if($scope.datas != undefined) {
						$scope.datas.goods = $scope.datas.goods.concat(data.result.goods);
						$scope.nPage += 1;
					} else {
						$scope.datas = data.result;
					}
				}
			},function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		/*==================下拉刷新 ===============*/
		
		
		
	}

	controller.$inject = ['$scope', 'Activitysecond', '$stateParams', '$timeout', 'GoPage', 'GoDialog', 'Auth', 'ChatInfo','$ionicModal','$sce'];

	return app.registerController("activitysecondctrl", controller);
});